package com.lvmama.conf;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.lvmama.entity.system.SysUser;

public class SessionStatusInterceptor implements HandlerInterceptor{
	

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		SysUser currentUser = (SysUser)request.getSession().getAttribute("currentUser");
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		System.out.println((authentication instanceof AnonymousAuthenticationToken));
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}
	public void sendJSONMsg(HttpServletResponse response, String json){
		response.setContentType("application/json;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
	    try {
	      PrintWriter out = response.getWriter();
	      out.write(json);
	      out.flush();
	      out.close();
	    } catch (Exception e) {
	      e.printStackTrace();
	    }
	}
}
